package com.zybzzz.workapp.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.zybzzz.workapp.R;
import com.zybzzz.workapp.fragment.PlanmsgFragment;

import java.util.UUID;

public class PlanMsgActivity extends AppCompatActivity {

    private static final String EXTRA_PLAN_ID=
            "com.zybzzz.workapp.activity.PlanMsgActivity.EXTRA_PLAN_ID";



    /**
     * @name: createFragment
     * @description: 原先重写的方法，现在不再继承抽象类
     * @return: androidx.fragment.app.Fragment
     * @date: 2020-10-21 22:16
     * @auther: zhangyibo
     *
    */
    protected Fragment createFragment() {
        UUID planid=(UUID) getIntent().getSerializableExtra(EXTRA_PLAN_ID);
        return PlanmsgFragment.makeNewInstance(planid);
    }


    /**
     * 提供静态方法，用于创建fragment跳转至mainactivity页面的intent
     * 在intent中添加用户计划的id信息，这样主界面才能对信息进行相关操作并且显示
     * @param: [packageContext, planid]
     * @return: android.content.Intent
     * @author: ZhangYiBo
     * @date: 2020/10/15
     */
    public static Intent makeNewIntentByPlanId(Context packageContext, UUID planid){
        Intent intent=new Intent(packageContext,PlanMsgActivity.class);
        intent.putExtra(EXTRA_PLAN_ID,planid);
        return intent;
    }

    /**
     * @name: onCreate
     * @description: 直接重写生命周期方法，目的是将添加界面脱离主界面,现在加载到AddMsg_Container
     * 暂时不考虑简化代码
     * @param savedInstanceState
     * @return: void
     * @date: 2020-10-21 22:17
     * @auther: zhangyibo
     *
    */
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_plan_msg);

        FragmentManager fm=getSupportFragmentManager();
        Fragment fragment=fm.findFragmentById(R.id.AddMsg_Container);

        if (fragment==null){
            fragment=createFragment();
            fm.beginTransaction()
                    .add(R.id.AddMsg_Container,fragment)
                    .commit();
        }
    }
}